package com.link.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.FetchMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.link.dao.LinkDAO;
import com.link.dao.LinkTypeDAO;
import com.link.dao.MappingAccountLinkDAO;
import com.link.po.Account;
import com.link.po.Link;
import com.link.po.LinkType;
import com.link.po.MappingAccountLink;
import com.others.util.Constants2;

public class LinkServiceImpl implements  LinkService {

    public List<Link> getLinkList() {
        return linkDAO.findAll();
    }
    
    public List<LinkType> getLinkTypeList() {
        return linkTypeDAO.findAll();
    }
    
    public List<Link> getLinkListByTypeId(int id) {

        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Link.class);
        detachedCriteria.setFetchMode("linkType", FetchMode.JOIN);
        detachedCriteria.add(Restrictions.eq("linkType.id", id));
        
        return linkDAO.findByCriteria(detachedCriteria);
    }
    
    public List<Link> getLinkListByAccountId(int id) {
        
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(MappingAccountLink.class);
        detachedCriteria.setFetchMode("account", FetchMode.JOIN);
        detachedCriteria.setFetchMode("link", FetchMode.JOIN);
        detachedCriteria.setFetchMode("link.linkType", FetchMode.JOIN);
        detachedCriteria.add(Restrictions.eq("deleteFlag", new Boolean(false)));
        detachedCriteria.add(Restrictions.eq("account.id", id));

        List<MappingAccountLink> mappingAccountLinkList = mappingAccountLinkDAO.findByCriteria(detachedCriteria);
        
        List<Link> linkList = new ArrayList<Link>();
        if (mappingAccountLinkList != null && !mappingAccountLinkList.isEmpty()) {
            
            for (MappingAccountLink mappingAccountLink : mappingAccountLinkList) {
                linkList.add(mappingAccountLink.getLink());
            }
        }
        
        return linkList;
    }
    
    public List<Link> getDefaultLinkList() {
        
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Link.class);
        detachedCriteria.add(Restrictions.eq("deleteFlag",  new Boolean(false)));
        detachedCriteria.setFetchMode("linkType", FetchMode.JOIN);
        detachedCriteria.addOrder(Order.asc("id"));
        
        detachedCriteria.add(Restrictions.le("id", Constants2.LINK_LIST_SIZE_DEFAULT));

        return linkDAO.findByCriteria(detachedCriteria);

    }
    
    public LinkType getLinkTypebyId(int id) {
        return linkTypeDAO.find(id);
    }
    
    public void saveLink(Link link) {
        linkDAO.save(link);
    }
    
    public void saveAccountLink(Link link, Account account) {
        
        linkDAO.save(link);
        
        MappingAccountLink mappingAccountLink = new MappingAccountLink();
        mappingAccountLink.setAccount(account);
        mappingAccountLink.setLink(link);
        mappingAccountLinkDAO.save(mappingAccountLink);
    }
    
    public void updateNumClick(int linkId) {
        Link link = linkDAO.find(linkId);
        
        int newNum = link.getNumClick() + 1;
        link.setNumClick(newNum);
        linkDAO.update(link);
    }
    
    private LinkDAO linkDAO;
    private LinkTypeDAO linkTypeDAO;
    private MappingAccountLinkDAO mappingAccountLinkDAO;
    
    public LinkDAO getLinkDAO() {
        return linkDAO;
    }
    public void setLinkDAO(LinkDAO linkDAO) {
        this.linkDAO = linkDAO;
    }
    public LinkTypeDAO getLinkTypeDAO() {
        return linkTypeDAO;
    }
    public void setLinkTypeDAO(LinkTypeDAO linkTypeDAO) {
        this.linkTypeDAO = linkTypeDAO;
    }

    public MappingAccountLinkDAO getMappingAccountLinkDAO() {
        return mappingAccountLinkDAO;
    }

    public void setMappingAccountLinkDAO(MappingAccountLinkDAO mappingAccountLinkDAO) {
        this.mappingAccountLinkDAO = mappingAccountLinkDAO;
    }
    
}
